/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.siswa;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class SiswaAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward siswaList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        HttpSession session = request.getSession(true);
        try {
            List listSiswa = siswaService.loadAll();
            request.setAttribute(SISConstants.LIST_SISWA, listSiswa);
            session.removeAttribute(SISConstants.LIST_SISWA_CARD);
            session.removeAttribute(SISConstants.LIST_SISWA_RELATION);
//            session.removeAttribute("listUser");
            session.removeAttribute(SISConstants.LIST_SISWA);
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.SISWA_LIST);
    }

    public ActionForward siswaAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        //        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
//        HttpSession session = request.getSession(true);
//        try {
//            List listUser = userService.loadAll();
//            request.setAttribute("listUser", listUser);
//        } catch (Exception ex) {
//            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
//            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
//            saveMessages(request, actionMessages);
//            return new ActionForward(mapping.getInput());
//        }
        return mapping.findForward(SISScreenConstants.SISWA_ADD);
    }

    public ActionForward siswaSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        SiswaForm siswaForm = (SiswaForm) form;
        HttpSession session = request.getSession(true);
        try {
            boolean kodeSiswaIsUsed = siswaService.cekIsEmptyKode(siswaForm.getKode());
            if (kodeSiswaIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.SISWA_ADD);
            } else {
                Siswa siswa = new Siswa();
                siswa.setKode(siswaForm.getKode());
                siswa.setNama(siswaForm.getNama());
                siswa.setAlamat(siswaForm.getAlamat());
                siswa.setTelp(siswaForm.getTelp());
                siswa.setGolonganDarah(siswaForm.getGolonganDarah());
//                User user = userService.loadById(siswaForm.getUser());
//                siswa.setUser(user);
                siswa.setSiswaCard((List) session.getAttribute(SISConstants.LIST_SISWA_CARD));
                siswa.setSiswaRelations((List) session.getAttribute(SISConstants.LIST_SISWA_RELATION));
                siswaService.saveSiswa(siswa);
                session.removeAttribute(SISConstants.LIST_SISWA_CARD);
                session.removeAttribute(SISConstants.LIST_SISWA_RELATION);
//                session.removeAttribute("listUser");
                session.removeAttribute(SISConstants.LIST_SISWA);
                siswaForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.SISWA);
    }

    public ActionForward siswaEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        HttpSession session = request.getSession(true);
        SiswaForm siswaForm = (SiswaForm) form;
        try {
            session.setAttribute("siswaId", request.getParameter(SISConstants.ID).toString());
            Siswa siswa = (Siswa) siswaService.loadById(Long.parseLong((String) session.getAttribute("siswaId")));
            siswaForm.setKode(siswa.getKode());
            siswaForm.setNama(siswa.getNama());
            siswaForm.setAlamat(siswa.getAlamat());
            siswaForm.setTelp(siswa.getTelp());
            siswaForm.setGolonganDarah(siswa.getGolonganDarah());
            siswaForm.setUser(siswa.getUser().getId());
//            List listUser = userService.loadAll();
//            session.setAttribute("listUser", listUser);
            session.setAttribute(SISConstants.LIST_SISWA, siswaForm);
            if (session.getAttribute(SISConstants.LIST_SISWA_CARD) == null) {
                session.setAttribute(SISConstants.LIST_SISWA_CARD, siswa.getSiswaCard());
            }
            if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) == null) {
                session.setAttribute(SISConstants.LIST_SISWA_RELATION, siswa.getSiswaRelations());
            }
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mapping.findForward("siswaEdit");
    }

    public ActionForward siswaUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        HttpSession session = request.getSession(true);
        SiswaForm siswaForm = (SiswaForm) form;
        try {
            boolean kodeSiswaIsUsed = siswaService.cekIsEmptyKode(siswaForm.getKode());
            if (kodeSiswaIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward("siswaEdit");
            } else {
                Siswa siswa = siswaService.loadById(siswaForm.getId());
                siswa.setKode(siswaForm.getKode());
                siswa.setNama(siswaForm.getNama());
                siswa.setAlamat(siswaForm.getAlamat());
                siswa.setTelp(siswaForm.getTelp());
                siswa.setGolonganDarah(siswaForm.getGolonganDarah());
//                User user = userService.loadById(siswaForm.getUser());
//                siswa.setUser(user);
                siswa.setSiswaCard((List) session.getAttribute(SISConstants.LIST_SISWA_CARD));
                siswa.setSiswaRelations((List) session.getAttribute(SISConstants.LIST_SISWA_RELATION));
                siswaService.updateSiswa(siswa);
                session.removeAttribute(SISConstants.LIST_SISWA_CARD);
                session.removeAttribute(SISConstants.LIST_SISWA_RELATION);
//                session.removeAttribute("listUser");
                session.removeAttribute(SISConstants.LIST_SISWA);
                siswaForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.SISWA);
    }

    public ActionForward siswaDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        try {
            Siswa siswa = siswaService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            siswaService.deleteSiswa(siswa);
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.SISWA);
    }

    public ActionForward siswaDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        try {
            Siswa detailSiswa = (Siswa) siswaService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            request.setAttribute(SISConstants.DETAIL_SISWA, detailSiswa);
            request.setAttribute(SISConstants.LIST_SISWA_CARD, detailSiswa.getSiswaCard());
            request.setAttribute(SISConstants.LIST_SISWA_RELATION, detailSiswa.getSiswaRelations());
        } catch (Exception ex) {
            Logger.getLogger(SiswaAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mapping.findForward(SISScreenConstants.SISWA_DETAIL);
    }

    public ActionForward siswaCardAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) == null) {
            List listSiswaCard = new ArrayList();
            session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_ADD);
    }

    public ActionForward siswaCardSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) != null) {
            List listSiswaCard = (List) session.getAttribute(SISConstants.LIST_SISWA_CARD);
            SiswaForm siswaForm = (SiswaForm) form;
            boolean cek = true;
            for (int x = 0; x < listSiswaCard.size(); x++) {
                SiswaCard siswaCardTemp = (SiswaCard) listSiswaCard.get(x);
                if (siswaForm.getNoCard().equals(siswaCardTemp.getNoCard()) && siswaForm.getNamaCard().equals(siswaCardTemp.getNama())) {
                    cek = false;
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CARD_IS_EXIST));
                    saveMessages(request, actionMessages);
                    return mapping.findForward(SISScreenConstants.SISWA_CARD_ADD);
                }
            }
            if (cek == true) {
                SiswaCard siswaCard = new SiswaCard();
                siswaCard.setNoCard(siswaForm.getNoCard());
                siswaCard.setNama(siswaForm.getNamaCard());
                listSiswaCard.add(siswaCard);
                session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
                siswaForm.reset(mapping, request);
            }
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_ADD);
    }

    public ActionForward siswaCardEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) == null) {
            List listSiswaCard = new ArrayList();
            session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_EDIT);
    }

    public ActionForward siswaCardUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) != null) {
            List listSiswaCard = (List) session.getAttribute(SISConstants.LIST_SISWA_CARD);
            SiswaForm siswaForm = (SiswaForm) form;
            boolean cek = true;
            for (int x = 0; x < listSiswaCard.size(); x++) {
                SiswaCard siswaCardTemp = (SiswaCard) listSiswaCard.get(x);
                if (siswaForm.getNoCard().equals(siswaCardTemp.getNoCard()) && siswaForm.getNamaCard().equals(siswaCardTemp.getNama())) {
                    cek = false;
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CARD_IS_EXIST));
                    saveMessages(request, actionMessages);
                    return mapping.findForward(SISScreenConstants.SISWA_CARD_EDIT);
                }
            }
            if (cek == true) {
                SiswaCard siswaCard = new SiswaCard();
                siswaCard.setNoCard(siswaForm.getNoCard());
                siswaCard.setNama(siswaForm.getNamaCard());
                listSiswaCard.add(siswaCard);
                session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
                siswaForm.reset(mapping, request);
            }
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_EDIT);
    }

    public ActionForward siswaCardAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) != null) {
            List listSiswaCard = (List) session.getAttribute(SISConstants.LIST_SISWA_CARD);
            for (int x = 0; x < listSiswaCard.size(); x++) {
                SiswaCard siswaCard = (SiswaCard) listSiswaCard.get(x);
                if (siswaCard.getNoCard().equals(request.getParameter(SISConstants.NO_CARD))) {
                    listSiswaCard.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_ADD);
    }

    public ActionForward siswaCardEditDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_CARD) != null) {
            List listSiswaCard = (List) session.getAttribute(SISConstants.LIST_SISWA_CARD);
            for (int x = 0; x < listSiswaCard.size(); x++) {
                SiswaCard siswaCard = (SiswaCard) listSiswaCard.get(x);
                if (siswaCard.getNoCard().equals(request.getParameter(SISConstants.NO_CARD))) {
                    listSiswaCard.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_SISWA_CARD, listSiswaCard);
        }
        return mapping.findForward(SISScreenConstants.SISWA_CARD_EDIT);
    }

    public ActionForward siswaRelationAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) == null) {
            List listSiswaRelation = new ArrayList();
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_ADD);
    }

    public ActionForward siswaRelationSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) != null) {
            List listSiswaRelation = (List) session.getAttribute(SISConstants.LIST_SISWA_RELATION);
            SiswaForm siswaForm = (SiswaForm) form;
            SiswaRelations siswaRelations = new SiswaRelations();
            siswaRelations.setNama(siswaForm.getNamaRelation());
            siswaRelations.setUmur(siswaForm.getUmur());
            siswaRelations.setRelasi(siswaForm.getRelasi());
            listSiswaRelation.add(siswaRelations);
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
            siswaForm.reset(mapping, request);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_ADD);
    }

    public ActionForward siswaRelationEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) == null) {
            List listSiswaRelation = new ArrayList();
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_EDIT);
    }

    public ActionForward siswaRelationUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) != null) {
            List listSiswaRelation = (List) session.getAttribute(SISConstants.LIST_SISWA_RELATION);
            SiswaForm siswaForm = (SiswaForm) form;
            SiswaRelations siswaRelations = new SiswaRelations();
            siswaRelations.setNama(siswaForm.getNamaRelation());
            siswaRelations.setUmur(siswaForm.getUmur());
            siswaRelations.setRelasi(siswaForm.getRelasi());
            listSiswaRelation.add(siswaRelations);
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
            siswaForm.reset(mapping, request);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_EDIT);
    }

    public ActionForward siswaRelationAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) != null) {
            List listSiswaRelation = (List) session.getAttribute(SISConstants.LIST_SISWA_RELATION);
            for (int x = 0; x < listSiswaRelation.size(); x++) {
                SiswaRelations siswaRelations = (SiswaRelations) listSiswaRelation.get(x);
                if (siswaRelations.getNama().equals(request.getParameter(SISConstants.NAMA))) {
                    listSiswaRelation.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_ADD);
    }

    public ActionForward siswaRelationEditDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_SISWA_RELATION) != null) {
            List listSiswaRelation = (List) session.getAttribute(SISConstants.LIST_SISWA_RELATION);
            for (int x = 0; x < listSiswaRelation.size(); x++) {
                SiswaRelations siswaRelations = (SiswaRelations) listSiswaRelation.get(x);
                if (siswaRelations.getNama().equals(request.getParameter(SISConstants.NAMA))) {
                    listSiswaRelation.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_SISWA_RELATION, listSiswaRelation);
        }
        return mapping.findForward(SISScreenConstants.SISWA_RELATION_EDIT);
    }
}